package com.nineclock.document.controller;


import com.nineclock.common.entity.PageResult;
import com.nineclock.common.entity.Result;
import com.nineclock.document.dto.*;
import com.nineclock.document.service.DocCollaborationsService;
import com.nineclock.document.service.DocFileHistoryService;
import com.nineclock.document.service.DocFileService;
import com.nineclock.document.service.DocFolderService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@Api(value = "APP文件管理接口", tags = "文档服务")
public class DocumentController {

    @Autowired
    private DocFileService docFileService;

    @Autowired
    private DocFolderService folderService;

    @Autowired
    private DocCollaborationsService collaborationsService;

    @Autowired
    private DocFileHistoryService docFileHistoryService;

    /**
     * 查询当前目录下的文件夹及文件
     * 接口路径：GET/document/listFolderAndFile
     */
    @ApiOperation("查询当前目录下的文件夹及文件")
    @GetMapping("listFolderAndFile")
    public Result<DocumentDto> queryFolderAndFile(@RequestParam(defaultValue = "0") Long parentFoldId) {
        DocumentDto documentDto = docFileService.queryFolderAndFile(parentFoldId);
        return Result.success(documentDto);
    }

    /**
     * 新增文件夹
     * 接口路径：POST/document/insertFolder
     */
    @ApiOperation("新增文件夹")
    @PostMapping("insertFolder")
    public Result insertFolder(@RequestBody DocFolderDto folderDto){
        folderService.insertFolder(folderDto);
        return Result.success();
    }


    /**
     * 根据文档ID查询文档
     * 接口路径：GET/document/getFileByFileId
     */
    @ApiOperation("根据文档ID查询文档")
    @GetMapping("getFileByFileId")
    public Result<DocFileDto> getFileByFileId(Long id){
        DocFileDto fileDto = docFileService.getFileByFileId(id);
        return Result.success(fileDto);
    }

    /**
     * 查询文档协作者列表
     * 接口路径：GET/document/pagingCollaborations
     */
    @ApiOperation("查询文档协作者列表")
    @GetMapping("pagingCollaborations")
    public Result<PageResult<DocumentUserDTO>> pagingCollaborations(Long id,
                                                                    @RequestParam(defaultValue = "1") Integer page,
                                                                     @RequestParam(defaultValue = "7") Integer pageSize){
        PageResult<DocumentUserDTO> pageResult = collaborationsService.pagingCollaborations(id, page, pageSize);
        return Result.success(pageResult);
    }


    /**
     * 根据文件ID获取员工列表
     * 接口路径：GET/document/pagingUsers
     */
    @ApiOperation("根据文件ID获取员工列表")
    @GetMapping("pagingUsers")
    public Result<List<DocUserCollaborationDTO>> pagingUsers(Long id){
        List<DocUserCollaborationDTO> userCollaborationDTOList =  collaborationsService.pagingUsers(id);
        return Result.success(userCollaborationDTOList);
    }





    /**
     * 分页查询文件列表
     * 接口路径：GET/document/pagingFile
     */
    @GetMapping("pagingFile")
    @ApiOperation("分页查询文件列表")
    public Result<PageResult<DocFileDto>> queryPagingFile(@RequestParam(defaultValue = "1") Long page,
                                                          @RequestParam(defaultValue = "10") Long pageSize,
                                                          @RequestParam(required = false) String keyword){
        PageResult<DocFileDto> pageResult = docFileService.queryPagingFile(page,pageSize,keyword);
        return Result.success(pageResult);
    }

    /**
     * 查询文件历史记录
     * 接口路径：GET/document/history/{fileId}
     */
    @GetMapping("history/{fileId}")
    @ApiOperation("查询文件历史记录")
    public Result<List<DocFileHistoryDto>> queryFileHistory(@PathVariable("fileId") Long fileId){
        List<DocFileHistoryDto> fileHistoryDtoList = docFileHistoryService.queryFileHistory(fileId);
        return Result.success(fileHistoryDtoList);
    }

    /**
     * 封禁/解封文件
     * 接口路径：PUT/document/{fileId}/{status}
     */
    @PutMapping("/{fileId}/{status}")
    @ApiOperation("封禁/解封文件")
    public Result bannedFile(@PathVariable("fileId") Long fileId,@PathVariable("status") short status){
        docFileService.bannedFile(fileId,status);
        return Result.success();
    }
}
